package net.peihuan.controller;

import net.peihuan.entity.Friendlink;
import net.peihuan.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/link")
public class LinkController {

    @Autowired
    private LinkService linkService;

    //查询所有友链
    @RequestMapping("/findAllFriendLinks")
    @ResponseBody
    List<Friendlink> findAllFriendLinks(){
        return linkService.findAllFriendLinks();
    }

    //删除友链
    @RequestMapping("/delete")
    @ResponseBody
    String delete(HttpServletRequest request)
    {
        int id =Integer.parseInt(request.getParameter("id"));
        if(linkService.removeFriendLink(id)==1){
            return"success";
        }else{
            return "failed";
        }
    }

    //添加友链
    @RequestMapping("/addLink")
    @ResponseBody
    int addLink(@RequestBody Friendlink friendlink){
        return linkService.addFriendLink(friendlink);
    }

    //查询友链的个数
    @RequestMapping("/count")
    @ResponseBody
    public int countTag(){
        return linkService.count();
    }

    //更新友链
    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody Friendlink friendlink){
        if(linkService.update(friendlink)==1){
            return "success";
        }
        return "failed";
    }
    //查询某个友链
    @RequestMapping("/selectLink")
    @ResponseBody
    public Friendlink selectLink(HttpServletRequest request){
        int id =Integer.parseInt(request.getParameter("id"));
        return linkService.findLink(id);
    }

}
